package com.inei.reportedeverificacion.view;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.inei.reportedeverificacion.R;
import com.inei.reportedeverificacion.controller.HogarController;
import com.inei.reportedeverificacion.controller.MarcoController;
import com.inei.reportedeverificacion.model.entity.Hogar;
import com.inei.reportedeverificacion.model.entity.Marco;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class Vivienda_Activity extends Activity {

	// compoenentes
	private TextView tvEncN, tvEncDni, tvSupN, tvSupDni, tvFecha,
			tvConglomerado, tvGrupo, tvVivienda;
	private ListView lvHogares;

	// variables
	private MarcoController mcon;
	private Marco mar;
	private HogarController hcon;
	private List<Hogar> hog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_vivienda);

		mcon = new MarcoController(Vivienda_Activity.this);
		hcon = new HogarController(Vivienda_Activity.this);

		IniciarComponentes();

		CargarDatos();
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		getMenuInflater().inflate(R.menu.hogar_menu, menu);
		menu.setHeaderTitle("Menu");
		super.onCreateContextMenu(menu, v, menuInfo);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.mnEditar:
			Intent ir_reporte = new Intent(Vivienda_Activity.this,
					Fragment_Seccion_1.class);
			Bundle datos = getIntent().getExtras();
			datos.putString("CLAVEVIVHOGAR", hog.get(info.position)
					.getCLAVEVIVHOGAR());
			ir_reporte.putExtras(datos);
			startActivity(ir_reporte);
			finish();

			break;
		default:
			break;
		}
		return super.onContextItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		Intent ir_marco = new Intent(this, Marco_Activity.class);
		startActivity(ir_marco);
		finish();
		super.onBackPressed();
	}

	private void IniciarComponentes() {
		tvEncN = (TextView) findViewById(R.id.tvEncN);
		tvEncDni = (TextView) findViewById(R.id.tvEncDni);
		tvSupN = (TextView) findViewById(R.id.tvSupN);
		tvSupDni = (TextView) findViewById(R.id.tvSupDni);
		tvFecha = (TextView) findViewById(R.id.tvFecha);
		tvConglomerado = (TextView) findViewById(R.id.tvConglomerado);
		tvGrupo = (TextView) findViewById(R.id.tvGrupo);
		tvVivienda = (TextView) findViewById(R.id.tvVivienda);

		lvHogares = (ListView) findViewById(R.id.lvHogares);
		registerForContextMenu(lvHogares);
	}

	private void CargarDatos() {
		mar = new Marco();
		mar.setCLAVEVIV(getIntent().getExtras().getString("CLAVEVIV"));
		mar = mcon.MarcoBuscar(mar);

		if (mar != null) {
			if (mar.getC1P17_1B() != null) {
				tvEncN.setText(mar.getC1P17_1B());
			}
			if (mar.getC1P17_1A() != null) {
				tvEncDni.setText(mar.getC1P17_1A());
			}
			if (mar.getC1P17_2B() != null) {
				tvSupN.setText(mar.getC1P17_2B());
			}
			if (mar.getC1P17_2A() != null) {
				tvSupDni.setText(mar.getC1P17_2A());
			}
			if (mar.getCONGLOMERADO() != null) {
				tvConglomerado.setText(mar.getCONGLOMERADO());
			}
			if (mar.getNSELV() != null) {
				tvVivienda.setText(mar.getNSELV());
			}
		}

		hog = hcon.HogarLeerViv(getIntent().getExtras().getString("CLAVEVIV"));

		if (hog != null) {
			SimpleAdapter adapter = new SimpleAdapter(this, convertir(hog),
					R.layout.hogar_item, new String[] { "cod_hogar", "hogar" },
					new int[] { R.id.tvHogar, R.id.tvCod });
			lvHogares.setAdapter(adapter);
			lvHogares.setDividerHeight(0);
		}
	}

	public List<HashMap<String, String>> convertir(List<Hogar> l) {
		List<HashMap<String, String>> lista = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < l.size(); i++) {
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("cod_hogar", "HOGAR " + (i + 1));
			map.put("hogar", "" + l.get(i).getHOGAR());
			lista.add(map);
		}
		return lista;
	}
}
